﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace T01WPF
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            GameUserData.ItemsSource = UserDao.LoadDate();
        }
        private void UserEvent_Click(object sender,RoutedEventArgs eventArgs)
        { 
            switch ((sender as Control).Name)
            {
                case "About":
                    MessageBox.Show("这是mysql数据访问测试程序", "关于");
                    break;
                case "Exit":
                    this.Close();
                    Application.Current.Shutdown(0);
                    break;
                case "UserInsert":
                    var saveDialog = new SaveUserDialog();
                    saveDialog.ActionType = "insert";
                    saveDialog.SaveUser.Content = "插入用户";
                    if(saveDialog.ShowDialog()==true)
                    {
                        GameUserData.ItemsSource = UserDao.LoadDate();
                    }
                    break;
                case "UserFind":
                    FindUserDialog findDia = new FindUserDialog();
                    if(findDia.ShowDialog()==true)
                    {
                        GameUserData.ItemsSource = findDia.user;
                    }
                    break;
                case "UserUpdate":
                    if(GameUserData.SelectedItem is User)
                    {
                        User user = GameUserData.SelectedItem as User;
                        var updateDialog = new SaveUserDialog(user);
                        updateDialog.ActionType = "update";
                        updateDialog.SaveUser.Content = "修改用户";
                        if(updateDialog.ShowDialog()==true)
                        {
                            GameUserData.ItemsSource = UserDao.LoadDate();
                        }
                    }
                    break;
                case "UserDelete":
                    MessageBoxResult confirmDelete = MessageBox.Show("你确定要删除所选的数据选项吗?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if(confirmDelete==MessageBoxResult.Yes)
                    {
                        if(GameUserData.SelectedItem is User)
                        {
                            var _user = GameUserData.SelectedItem as User;
                            UserDao.Delete(_user.Id);
                            GameUserData.ItemsSource = UserDao.LoadDate();
                        }
                    }
                    break;
            }
        }
    }
}
